<?php
	
function list_bank(){

//PASANG JAVASCRIPT
my_set_code_js('
	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com=bank&task=delete_bank&id=\'+id;
		}
		return false;
	}
');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Singkatan'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		'Nama Bank'=>array('style'=>'border-bottom:2px solid;width:70%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM bank ORDER BY singkat";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=bank&task=edit_bank&id='.$ey['bank_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['bank_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'singkat'=>  $ey['singkat'] ,   
			'bank'=>  $ey['bank'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);   
	return table_builder($header , $datas , 4 , false );
}

function form_bank($id = 0){
	$view = form_header( "bank" , "bank"  );
 	$fields = my_get_data_by_id('bank','bank_id',$id);

	$temp = array(
		'name'=>'singkat',
		'value'=>( isset($_POST['singkat']) ? $_POST['nama'] : $fields['singkat'] ),
		'id'=>'singkat',
		'type'=>'text',
		'size'=>'10'
	);
	$form_temp = form_dynamic($temp);	
	$view .= form_field_display( $form_temp  , "Singkatan"   );	

	$temp = array(
		'name'=>'bank',
		'value'=>( isset($_POST['bank']) ? $_POST['bank'] : $fields['bank'] ),
		'id'=>'bank',
		'type'=>'text',
		'size'=>'45'
	);
	$form_temp = form_dynamic($temp);	
	$view .= form_field_display( $form_temp   , "Nama Bank"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function valid_bank($id){
	$errsubmit = false;
	$err = array();
	 
	$temp =  trim($_POST["singkat"]); 
	if( $temp == ''){
		$errsubmit = true;
		$err[] = "Singkatan belum diisi";
	}else{
		$query="SELECT bank_id FROM bank WHERE singkat='{$temp}' AND bank_id<>{$id}"; 
		if (my_num_rows( my_query($query)) >0){
			$errsubmit = true;
			$err[] = "Singkatan sudah ada";	
		}	
	}
	
	$temp =  trim($_POST["bank"]); 
	if( $temp == ''){
		$errsubmit = true;
		$err[] = "Nama bank belum diisi";
	}else{
		$query="SELECT bank_id FROM bank WHERE bank='{$temp}' AND bank_id<>{$id}"; 
		if (my_num_rows( my_query($query)) >0){
			$errsubmit = true;
			$err[] = "Nama bank sudah ada";	
		}	
	}	
	if( $errsubmit){
		return $err;
	}	
	return $errsubmit;
}

function submit_bank($id){
	$datas = array(
		'singkat'=>my_type_data_str( $_POST['singkat'] ),
		'bank'=>my_type_data_str( $_POST['bank'] ),
		'datetime_added'=>my_type_data_function('NOW()'),
	);
	if($id > 0){
		return my_update_record('bank' ,'bank_id' , $id , $datas);
	}
	
	return my_insert_record('bank' , $datas);
}


function remove_bank($id){
	$query= "DELETE FROM bank WHERE bank_id = {$id}";
	return my_query($query);
} 
 